//
//  ALi-ViewController.m
//  22-homework-QQMV-ALi
//
//  Created by 王道林 on 16/8/13.
//  Copyright © 2016年 王道林. All rights reserved.
//

#define COLORAUTO [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0]

#import "ALi-ViewController.h"
#import "BaseCollectionView.h"
#import "WHSNetworkParser.h"
#import "AL-DataModel.h"
#import "AL_CycleCollectionReusableView.h"
#import "AL_CollectionViewCell_OneClass.h"
#import "AL_CollectionViewCell_Recomend.h"
#import "AL_CollectionReusableView_HeaderT.h"
#import "AL_CollectionViewCell_Show.h"
#import "AL_CollectionViewCell_Only.h"
#import "AL_ViewController_SingDetail.h"
@interface ALi_ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) BaseCollectionView *CollectionView;
@property (nonatomic, strong) NSMutableArray *DataArr;


@end

@implementation ALi_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createData];
    [self createSubView];
    // Do any additional setup after loading the view.
}

- (void)createData
{
    [WHSNetworkParser GETWithURL:@"http://api.dongting.com/frontpage/frontpage?location=0&version=1470998222737&app=ttpod&v=v9.0.4.2016070616&user_id=0&mid=iPhone8%2C1&f=f320&s=s310&imsi=&hid=&splus=10.0&active=1&net=2&openudid=4bbd8925c4a3991ee51c9cd8b9a8dc5961820764&idfa=2BBAE4F7-2E3B-41AA-AE0C-F91FB2BDC279&utdid=V62kivWPJBYDABK%2FL8gCUnDI&alf=(null)&bundle_id=com.ttpod.music&latitude=(null)&longtitude=(null)&access_token=(null)&vc=9000400" par:nil success:^(id responseObject) {

        _DataArr = [NSMutableArray array];
        for (NSDictionary *dic in responseObject[@"data"]) {
            AL_DataModel *model = [AL_DataModel modelWithDic:dic];
            [_DataArr addObject:model];
        }
        [_CollectionView reloadData];
        
    } filed:^(NSError *error) {
        
    }];
}


- (void)createSubView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;
    _CollectionView = [[BaseCollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    _CollectionView.backgroundColor = [UIColor colorWithRed:0.059 green:0.075 blue:0.094 alpha:1.000];
    _CollectionView.delegate = self;
    _CollectionView.dataSource = self;
    _CollectionView.contentInset = UIEdgeInsetsMake(0, 0, 64, 0);
    [self.view addSubview:_CollectionView];
    // 注册
    [_CollectionView registerNib:[UINib nibWithNibName:@"AL_CollectionViewCell_Only" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"PoolOnly"];
    // 注册头轮播
    [_CollectionView registerClass:[AL_CycleCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CyclePoolOne"];
    // 注册section1的cell
    [_CollectionView registerNib:[UINib nibWithNibName:@"AL_CollectionViewCell_OneClass" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ClassOnePool"];
    // 注册section2的cell
    [_CollectionView registerNib:[UINib nibWithNibName:@"AL_CollectionViewCell_Recomend" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ClassTwoPool"];
    // 注册分区头
    [_CollectionView registerNib:[UINib nibWithNibName:@"AL_CollectionReusableView_HeaderT" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolHeaderT"];
    
    // 注册cellShow
    [_CollectionView registerNib:[UINib nibWithNibName:@"AL_CollectionViewCell_Show" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"ClassPoolShow"];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return _DataArr.count;
}
// 分区中的item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 0) {
        return 0;
    } else {
        AL_DataModel *model = _DataArr[section];
        return model.data.count;
    }
}
// 不同的cell不同的大小

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    switch (indexPath.section) {
        case 0:
            return CGSizeMake(0, 0);
            break;
            case 1:
            return CGSizeMake(self.view.frame.size.width / 3, 100);
            break;
            case 2:
            return CGSizeMake(self.view.frame.size.width / 2, 100);
            case 7:
            return CGSizeMake(self.view.frame.size.width - 20, 80);
        default:
            return CGSizeMake(self.view.frame.size.width / 3, 160);
            break;
    }
}


// 返回不同的cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1) {
        AL_CollectionViewCell_OneClass *cellOne = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClassOnePool" forIndexPath:indexPath];
        AL_DataModel *dataModel = _DataArr[indexPath.section];
      AL_ModelPic *modelPic = dataModel.data[indexPath.row];
        cellOne.modelPic = modelPic;
        return cellOne;
    } else if (indexPath.section == 2)
    {
        AL_CollectionViewCell_Recomend *cellTwo = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClassTwoPool" forIndexPath:indexPath];
        AL_DataModel *dataModel = _DataArr[indexPath.section];
        AL_ModelPic *modelPic = dataModel.data[indexPath.row];
        cellTwo.ModelPic = modelPic;
        return cellTwo;
    } else if (2 < indexPath.section && indexPath.section < 7)
    {
        AL_CollectionViewCell_Show *CellShow = [collectionView dequeueReusableCellWithReuseIdentifier:@"ClassPoolShow" forIndexPath:indexPath];
        AL_DataModel *dataModel = _DataArr[indexPath.section];
        AL_ModelPic *modelPic = dataModel.data[indexPath.row];
        CellShow.modelPic = modelPic;
        return CellShow;
    } else
    {
     AL_CollectionViewCell_Only *cellOnly = [collectionView dequeueReusableCellWithReuseIdentifier:@"PoolOnly" forIndexPath:indexPath];
    AL_DataModel *dataModel = _DataArr[indexPath.section];
    AL_ModelPic *modelPic = dataModel.data[indexPath.row];
    cellOnly.modelPic = modelPic;
    return cellOnly;
    }
}

// 返回头的大小.
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        
        return CGSizeMake(self.view.frame.size.width, 180);
    } else if (section == 1)
    {
        return CGSizeMake(0, 0);
    }
    return CGSizeMake(self.view.frame.size.width, 60);
}

//  返回头的view
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
       AL_CycleCollectionReusableView *view = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"CyclePoolOne" forIndexPath:indexPath];
        view.dataModel = _DataArr[indexPath.section];
        return view;
    }
    else if (indexPath.section > 1)
    {
        AL_CollectionReusableView_HeaderT *ViewHeader = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"poolHeaderT" forIndexPath:indexPath];
        ViewHeader.dataModel = _DataArr[indexPath.section];
        return ViewHeader;
    }
    return nil;
}




- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section < 7 && indexPath.section > 2) {
        
        AL_DataModel *dataModel = _DataArr[indexPath.section];
        AL_ModelPic *modelPic = dataModel.data[indexPath.row];
        
      NSString *value = modelPic.action[@"value"];
        AL_ViewController_SingDetail *VCSingDetail = [[AL_ViewController_SingDetail alloc] init];
        VCSingDetail.value = value;
        [self.navigationController pushViewController:VCSingDetail animated:YES];
        
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
